import { fabric } from 'fabric';

export function renderIcon(icon) {
  return function renderIcon(
    this: fabric.Control,
    ctx,
    left,
    top,
    styleOverride,
    fabricObject
  ): void {
    const size = this.sizeX;
    ctx.save();
    ctx.translate(left, top);
    ctx.rotate(fabric.util.degreesToRadians(fabricObject.angle));
    ctx.drawImage(icon, -size / 2, -size / 2, size, size);
    ctx.restore();
  };
}

export function loadImgFromSvg(svg): HTMLImageElement {
  const xml = new XMLSerializer().serializeToString(svg);
  const img = document.createElement('img');
  img.src = 'data:image/svg+xml;base64,' + window.btoa(xml);
  return img;
}
